package com.msb.mall.ware.service.impl;

import com.msb.mall.common.dto.SkuHasStockDto;
import com.msb.mall.common.utils.R;
import com.msb.mall.ware.feign.ProductFeignService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.msb.mall.common.utils.PageUtils;
import com.msb.mall.common.utils.Query;

import com.msb.mall.ware.dao.WareSkuDao;
import com.msb.mall.ware.entity.WareSkuEntity;
import com.msb.mall.ware.service.WareSkuService;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Autowired
    private WareSkuDao wareSkuDao;
    @Autowired
    private ProductFeignService productFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareSkuEntity> queryWrapper = new QueryWrapper<>();
        String skuId = (String) params.get("skuId");
        if (StringUtils.isNotBlank(skuId)) {
            queryWrapper.eq("sku_id", skuId);
        }
        String wareId = (String) params.get("wareId");
        if (StringUtils.isNotBlank(wareId)) {
            queryWrapper.eq("ware_id", wareId);
        }
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    /**
     * 根据skuId和仓库id查询库存信息
     * @param skuId 商品id
     * @param wareId 仓库id
     * @param skuNum 商品数量
     */
    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        // 判断是否有改商品和仓库记录
        QueryWrapper<WareSkuEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sku_id", skuId);
        queryWrapper.eq("ware_id", wareId);
        List<WareSkuEntity> list = this.list(queryWrapper);
        if (list == null || list.size() == 0) {
            // 如果没有就新增库存记录
            WareSkuEntity wareSkuEntity = new WareSkuEntity();
            wareSkuEntity.setSkuId(skuId);
            wareSkuEntity.setWareId(wareId);
            wareSkuEntity.setStock(skuNum);
            wareSkuEntity.setStockLocked(0);
            R info = productFeignService.info(skuId);
            if (info.getCode() == 0) {
                Map<String, Object> skuInfo = (Map<String, Object>) info.get("skuInfo");
                wareSkuEntity.setSkuName(skuInfo.get("skuName").toString());
            }

            this.save(wareSkuEntity);
        } else {
            // 如果有就更新库存
            wareSkuDao.addStock(skuId, wareId, skuNum);
        }
    }

    /**
     * 获取每个skuId对应的库存
     * @param skuIds
     * @return
     */
    @Override
    public List<SkuHasStockDto> getSkusHasStock(List<Long> skuIds) {
        return skuIds.stream().map(skuId -> {
            Long count = this.baseMapper.getSkuStock(skuId);
            SkuHasStockDto skuHasStockDto = new SkuHasStockDto();
            skuHasStockDto.setSkuId(skuId);
            skuHasStockDto.setHasStock(count == null ? false : count > 0);   // 库存大于0有库存，否则没有库存
            return skuHasStockDto;
        }).collect(Collectors.toList());
    }
}